package com.test.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.test.comment.SingerBean;
import com.test.comment.UserBean;
import com.test.comment.UservoteBean;
import com.test.dao.SystemDao;

@Controller
@ResponseBody
@RequestMapping("/system")
public class SystemController {
	private Logger logger = Logger.getLogger(getClass());
    @Autowired
    SystemDao systemDao;
	@RequestMapping("/active")
	public ModelAndView goactive() {
		logger.debug("11111");
		return new ModelAndView("SystemAdmin");
	}
	
	@RequestMapping("/insertsingerrace")
	public Map<String, String> insertsingerrace(String singerName){
		logger.debug("=========singerName:"+singerName);
		systemDao.insertsinger(singerName);
		Map<String, String> map = new HashMap<>();
		return map;
	}
	@RequestMapping("/showsinger")
	public Map<String, Object> showsinger(){
		List<SingerBean> singerBeans = systemDao.allsinger();
		logger.debug("=============::"+singerBeans.get(0).getSingerName());
		Map<String, Object> map = new HashMap<>();
		map.put("tol", 0);
		map.put("message", "aaaaa");
		map.put("datasinger", singerBeans);
		map.put("status", 200);
	    return map;
	}
	@RequestMapping("/addsingerrace")
	public Map<String, String> addsingerrace(Integer sid,Integer rid, String singer){
		logger.debug("==========sid:"+sid);
		logger.debug("===========rid:"+rid);
		logger.debug("============singer:"+singer);
		systemDao.insertsingerrace(sid,rid,singer);
		Map<String, String> map = new HashMap<>();
		return map;
	}
	@RequestMapping("/showuser")
	public Map<String, Object> showuser(Integer page,Integer limit){
		int tmp=(page-1)*limit;
		int tol = systemDao.findtotal();
		List<UservoteBean> uservoteBeans = systemDao.finduser(tmp,limit);
		Map<String, Object> map = new HashMap<>();
		map.put("tol", tol);
		map.put("message", "success");
		map.put("uservoteBeans", uservoteBeans);
		map.put("status", 200);
		return map;
	}
	@RequestMapping("/updateuser")
	public Map<String, String> updateuser(String usercode,String field,String value){
		logger.debug("==========usercode:"+usercode);
		logger.debug("==========field:"+field);
		logger.debug("==========value:"+value);
		UserBean userBean = new UserBean();
		userBean.setUsercode(usercode);
		if (field.equals("username")) {
			userBean.setUsername(value);
		}else if (field.equals("password")) {
			userBean.setPassword(value);
		}else if (field.equals("usertmp")) {
			userBean.setUsertmp(value);
		}else if (field.equals("useremail")) {
			userBean.setUseremail(value);
		}
		systemDao.updateuser(userBean);
		Map<String, String> map = new HashMap<>();
		return map;
		
	}
	@RequestMapping("/deleteuser")
	public Map<String, String> deleteuser(String usercode){
		systemDao.deleteuser(usercode);
		Map<String, String> map = new HashMap<>();
		return map;
	}
	@RequestMapping("/insertUser")
	public Map<String,String> insertUser(UserBean userBean){
		logger.debug("========Usercode"+userBean.getUsercode());
		logger.debug("=============UserType"+userBean.getUserType());
		logger.debug("=============Password"+userBean.getPassword());
		logger.debug("=============Username"+userBean.getUsername());
		logger.debug("=============Useremail"+userBean.getUseremail());
		logger.debug("=============Usertmp"+userBean.getUsertmp());
		systemDao.insertuser(userBean);
		Map<String, String> map = new HashMap<>();
		return map;
		
	}
	
}
